/**
* Copyright (c) 2010, Attila Magyar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the Attila Magyar nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.uftp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * The J2SE version of FTPClient
 * @author zeroflag
 */
public class FTPClient extends FTPBase {

    private int timeout = 15;

    public FTPClient(String remoteHost, int remotePort) {
        super(remoteHost, remotePort);
    }

    public FTPClient(String remoteHost) {
        super(remoteHost);
    }

    public FTPClient() {
    }


    /**
     * set socket timeout
     * @param timeout socket timeout in sencond
     */
    public void setTimeout( int timeout ) throws FTPSocketException {
        if ( control.isConnected() ) {
            control.setTimeout( timeout * 1000 );
        }
        this.timeout = timeout * 1000;
    }

    @Override
    public void connect() throws FTPSocketException, InvalidReplyException {
        control.connect();
        setTimeout( timeout );
        // 220 Service ready for new user.
        control.readReply().validate( 220 ); // validate connection
    }

    public void upload( String localFile, String remoteFile ) throws InvalidReplyException, FTPSocketException, FileNotFoundException {
        FileInputStream fis = null;
        
        try {
            fis = new FileInputStream( localFile );
            upload( fis, remoteFile );
        } finally {
            if ( fis != null ) {
                try {
                    fis.close();
                } catch (IOException e) {
                    logger.exception( e, "Closing local file: IOException" );
                }
            }
        }
    }

    public void download( String remoteFile, String localFile ) throws FTPSocketException, InvalidReplyException, FileNotFoundException {
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream( localFile );
            download( remoteFile, fos );
        } finally {
            if ( fos != null ) {
                try {
                    fos.close();
                } catch (IOException ex) {
                    logger.exception( ex, "Closing local file: IOException" );
                }
            }
        }
    }
    
}

